package Action.Sys;

import Action.BackgroundThread.GcThread;
import Action.BackgroundThread.RecommendedbooksThread;
import Bean.AdministratorEntity;
import Bean.Log.UserLogEntity;
import Bean.Log.HistoricalData;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import tools.HibernateTools;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

public class Login extends ActionSupport {
    public String err="";
    String username ;
    String password;
    UserinfoEntity userinfoEntity;
    @Override
    public String execute() throws Exception {
        if (username==null&&password==null){
            //第一次启动
            return LOGIN;
        }
        Session session= HibernateTools.getSession();
        //用户查找
        try {
            userinfoEntity = (UserinfoEntity) session.createQuery("from UserinfoEntity where username= ?").setParameter(0, username).list().get(0);

        }catch (Exception e){
            err="用户名不正确";
            addActionError(err);
            System.out.println(err);
            HibernateTools.commit(session);
            return INPUT;
        }
        if (userinfoEntity==null){
            err="用户名不正确";
            addActionError(err);
            System.out.println(err);
            HibernateTools.commit(session);
            return ERROR;
        }
        if (!userinfoEntity.getPassword().equals(password))
        {
            err="密码错误";
            addActionError(err);
            System.out.println(err);
            HibernateTools.commit(session);
            return INPUT;
        }
//        userinfoEntity.setLastLoginTime(new Timestamp(System.currentTimeMillis()));//已经由对象类自动生成
        userinfoEntity.setLastLoginTime();
        session.saveOrUpdate(userinfoEntity);

        HistoricalData visterNumEntity=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (visterNumEntity==null){
            visterNumEntity=new HistoricalData();
            visterNumEntity.setTimestamp(tools.TimeTools.formatTime(0));
        }
        visterNumEntity.setVisterNum(visterNumEntity.getVisterNum()+1);
        session.saveOrUpdate(visterNumEntity);



        UserLogEntity.addLog(userinfoEntity, this);
        //用户信息传输
        ServletActionContext.getRequest().getSession().putValue("username", userinfoEntity.getUsername());
        ServletActionContext.getRequest().getSession().putValue("userid", userinfoEntity.getId());//登录检测对象--全局
        HibernateTools.commit(session);
        return SUCCESS;

    }

    public String ManagerLogin(){
        Session session=HibernateTools.getSession();
        try {
            GcThread.getInstance().StartGc();
            RecommendedbooksThread.starter();
        }catch (Exception e){
            //屏蔽后台线程的错误
        }

        if (username==null&&password==null){
            //第一次启动
            return "Manage"+INPUT;
        }

        AdministratorEntity administratorEntity= null;
        try {
            administratorEntity = (AdministratorEntity) session.createQuery("from AdministratorEntity where name=?").setParameter(0,username).getResultList().get(0);
        } catch (Exception e) {
            err="用户名不正确";
            addActionError(err);
            System.out.println(err);
            HibernateTools.commit(session);
            return "Manage"+ERROR;
        }

        if (administratorEntity==null){
            addActionError("用户不存在");
            HibernateTools.commit(session);
            return "Manage"+ERROR;
        }
        if (!administratorEntity.getPassword().equals(password)){
            addActionError("密码错误");
            HibernateTools.commit(session);
            return "Manage"+ERROR;
        }
        ServletActionContext.getRequest().getSession().putValue("adminname", administratorEntity.getName());
        ServletActionContext.getRequest().getSession().putValue("adminid", administratorEntity.getId());
        HistoricalData visterNumEntity=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (visterNumEntity==null){
            visterNumEntity=new HistoricalData();
            visterNumEntity.setTimestamp(tools.TimeTools.formatTime(0));
        }
        visterNumEntity.setVisterNum(visterNumEntity.getVisterNum()+1);
        session.saveOrUpdate(visterNumEntity);

        //管理员session时间应该延长单位s---24小时
        ServletActionContext.getRequest().getSession().setMaxInactiveInterval(24*60*60);
        HibernateTools.commit(session);
        return "Manage"+SUCCESS;
    }
    public String userlogout(){
        ServletActionContext.getRequest().getSession().putValue("userid","");

        return LOGIN;
    }
    public String managerlogout(){
        ServletActionContext.getRequest().getSession().putValue("adminid","");
        return LOGIN;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserinfoEntity getUserinfoEntity() {
        return userinfoEntity;
    }

    public void setUserinfoEntity(UserinfoEntity userinfoEntity) {
        this.userinfoEntity = userinfoEntity;
    }
}
